# =====================================================
# 测试 RSS 抓取功能
# =====================================================

$baseUrl = "http://localhost:8080"

Write-Host "🧪 测试 RSS 抓取功能" -ForegroundColor Green
Write-Host ("=" * 60)

# =====================================================
# 1. 获取现有订阅源列表
# =====================================================
Write-Host "`n📋 步骤 1: 获取订阅源列表" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" -Method GET
    $feeds = $response.data
    Write-Host "✅ 找到 $($feeds.Count) 个订阅源" -ForegroundColor Green
    
    if ($feeds.Count -eq 0) {
        Write-Host "❌ 没有订阅源,请先添加订阅源" -ForegroundColor Red
        exit 1
    }
    
    # 显示订阅源列表
    for ($i = 0; $i -lt $feeds.Count; $i++) {
        $feed = $feeds[$i]
        Write-Host "  $($i + 1). [$($feed.id)] $($feed.title)" -ForegroundColor Gray
        Write-Host "     URL: $($feed.url)" -ForegroundColor DarkGray
    }
    
    # 选择第一个订阅源进行测试
    $testFeed = $feeds[0]
    $testFeedId = $testFeed.id
    Write-Host "`n✅ 将测试订阅源: $($testFeed.title)" -ForegroundColor Green
}
catch {
    Write-Host "❌ 获取订阅源列表失败: $_" -ForegroundColor Red
    exit 1
}

# =====================================================
# 2. 手动刷新订阅源(抓取文章)
# =====================================================
Write-Host "`n📡 步骤 2: 手动刷新订阅源 (抓取文章)" -ForegroundColor Cyan
try {
    Write-Host "正在抓取: $($testFeed.url)" -ForegroundColor Gray
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$testFeedId/refresh" -Method POST
    Write-Host "✅ $($response.message)" -ForegroundColor Green
    
    # 等待抓取完成
    Write-Host "⏳ 等待 5 秒..." -ForegroundColor Gray
    Start-Sleep -Seconds 5
}
catch {
    Write-Host "❌ 刷新订阅源失败: $_" -ForegroundColor Red
    Write-Host "错误详情: $($_.ErrorDetails.Message)" -ForegroundColor Red
    exit 1
}

# =====================================================
# 3. 检查是否有新文章
# =====================================================
Write-Host "`n📰 步骤 3: 检查抓取的文章" -ForegroundColor Cyan
try {
    $url = "$baseUrl/api/articles?page=1&size=10"
    $response = Invoke-RestMethod -Uri $url -Method GET
    
    Write-Host "✅ 总文章数: $($response.data.total)" -ForegroundColor Green
    Write-Host "✅ 当前页文章数: $($response.data.articles.Count)" -ForegroundColor Green
    
    if ($response.data.articles.Count -gt 0) {
        Write-Host "`n📄 最新文章列表:" -ForegroundColor Cyan
        for ($i = 0; $i -lt [Math]::Min(5, $response.data.articles.Count); $i++) {
            $article = $response.data.articles[$i]
            Write-Host "  $($i + 1). $($article.title)" -ForegroundColor Gray
            Write-Host "     作者: $($article.author) | 发布时间: $($article.published_at)" -ForegroundColor DarkGray
        }
    }
    else {
        Write-Host "⚠️  没有抓取到文章,可能原因:" -ForegroundColor Yellow
        Write-Host "  1. RSS 源没有内容" -ForegroundColor Yellow
        Write-Host "  2. RSS 源格式不正确" -ForegroundColor Yellow
        Write-Host "  3. 网络连接问题" -ForegroundColor Yellow
    }
}
catch {
    Write-Host "❌ 获取文章列表失败: $_" -ForegroundColor Red
}

# =====================================================
# 4. 测试去重功能(再次刷新)
# =====================================================
Write-Host "`n🔁 步骤 4: 测试去重功能 (再次刷新)" -ForegroundColor Cyan
try {
    Write-Host "再次抓取: $($testFeed.url)" -ForegroundColor Gray
    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$testFeedId/refresh" -Method POST
    Write-Host "✅ $($response.message)" -ForegroundColor Green
    
    Start-Sleep -Seconds 3
    
    # 检查文章数是否增加
    $url = "$baseUrl/api/articles?page=1&size=10"
    $newResponse = Invoke-RestMethod -Uri $url -Method GET
    
    Write-Host "✅ 总文章数: $($newResponse.data.total)" -ForegroundColor Green
    
    if ($newResponse.data.total -eq $response.data.total) {
        Write-Host "✅ 去重功能正常: 没有重复文章被添加" -ForegroundColor Green
    }
    else {
        Write-Host "⚠️  文章数量有变化,可能有新文章发布" -ForegroundColor Yellow
    }
}
catch {
    Write-Host "⚠️  测试去重功能失败: $_" -ForegroundColor Yellow
}

# =====================================================
# 总结
# =====================================================
Write-Host "`n" -NoNewline
Write-Host ("=" * 60)
Write-Host "🎉 RSS 抓取功能测试完成!" -ForegroundColor Green
Write-Host "`n💡 提示:" -ForegroundColor Yellow
Write-Host "  - 如果没有抓取到文章,请检查服务器日志" -ForegroundColor Gray
Write-Host "  - 可以尝试添加其他 RSS 源进行测试" -ForegroundColor Gray
Write-Host "  - 推荐测试: 阮一峰的博客 https://www.ruanyifeng.com/blog/atom.xml" -ForegroundColor Gray
